<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="用户名" prop="userName">
            <el-input
              v-model="queryParams.username"
              placeholder="请输入管理员用户名"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input
              v-model="queryParams.status"
              placeholder="请输入状态"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>

        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >

        <el-table
          v-loading="loading"
          row-key="id"
          :data="adminList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="管理员ID" align="center" />
          <el-table-column prop="username" label="用户名" align="center" />
          <el-table-column prop="email" label="邮箱" align="center" />
          <el-table-column
            prop="sex"
            label="性别"
            align="center"
            :formatter="sex"
          />
          <el-table-column prop="roleName" label="角色" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleTopChange(scope.row)"
              />
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-select
                v-model="roleId"
                @change="handlesetRole(scope.row, roleId)"
              >
                <el-option
                  v-for="item in adminRoleList"
                  :label="item.roleName"
                  :key="item.roleId"
                  :value="item.roleId"
                ></el-option>
              </el-select>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input
                v-model="form.username"
                placeholder="请输入用户名"
                maxlength="15"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="form.password"
                placeholder="请输入密码"
                maxlength="15"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="昵称" prop="nickname">
              <el-input
                v-model="form.nickname"
                placeholder="请输入昵称"
                maxlength="15"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="邮箱" prop="email">
              <el-input
                v-model="form.email"
                placeholder="请输入邮箱"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdmin,
  changeStatus,
  delAdmin,
  getRoleSelectList,
  setAdminRole,
  addAdmin,
} from "@/api/system/admin";

export default {
  name: "Admin",
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: undefined,
        status: undefined,
      },
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      adminList: [],
      showSearch: true,
      adminRoleList: [],
      roleId: "",
      form: {},
      type: "",
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getRoleList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        description: undefined,
        id: undefined,
      };
      this.resetForm("form");
    },
    /** 新增用户 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      addAdmin(this.form).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    sex(row, index) {
      if (row.sex == 1) {
        return "男";
      } else if(row.sex == 0) {
        return "女";
      }
    },
    //管理员状态改变
    handleTopChange(row) {
      const text = row.status === "0" ? "启用" : "禁用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.username + '"管理员吗？')
        .then(function () {
          return changeStatus(row.id, row.status);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 查询管理员用户列表 */
    getList() {
      this.loading = true;
      listAdmin(this.queryParams).then((response) => {
        this.adminList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查找角色选择列表 */
    getRoleList() {
      this.loading = true;
      getRoleSelectList().then((response) => {
        this.adminRoleList = response;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal
        .confirm('是否确认删除管理员编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAdmin(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    handlesetRole(row, roleId) {
      const ids = row.id;
      setAdminRole(ids, roleId).then((response) => {
        this.getList();
        this.roleId = "";
      });
    },
  },
};
</script>
